package com.yimi.yinhepda.adapter.RecycleAdapter;

import android.content.Context;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import com.yimi.yinhepda.R;
import com.pda.ym.base.CommonRecycleAdapter;
import com.pda.ym.base.CommonViewHolder;
import com.yimi.yinhepda.db.table.FuncBtnTable;
import com.yimi.yinhepda.utils.Common;
import com.yimi.yinhepda.utils.MyItemTouchCallback;
import com.pda.ym.utils.SharedPreferUtil;

import java.util.Collections;
import java.util.List;

/**
 * @author: lj
 * Date: 2019/10/8 18:27
 * Description:常用功能适配器
 */
public class CommFuncAdapter extends CommonRecycleAdapter<FuncBtnTable> implements MyItemTouchCallback.ItemTouchAdapter {

    private Context mContext;
    private boolean showDeleteBtn;

    public CommFuncAdapter(Context context, List<FuncBtnTable> dataList) {
        super(context, dataList, R.layout.adapter_comm_func_item);
        this.mContext = context;

    }

    @Override
    public void bindData(CommonViewHolder holder, FuncBtnTable data, int position) {
        holder.getView(R.id.iv_func_pic).setBackgroundDrawable(mContext.getResources().getDrawable(data.getPicId()));
        holder.setText(R.id.tv_func_name, data.getName());
        if (showDeleteBtn && !data.getNameCode().equals(Common.FUNC_ADD)) {
            holder.getView(R.id.iv_delete_btn).setVisibility(View.VISIBLE);
            Animation shake;
            if (position % 2 == 1) {
                shake = AnimationUtils.loadAnimation(mContext, R.anim.anim_func);
            } else {
                shake = AnimationUtils.loadAnimation(mContext, R.anim.anim_func2);
            }
            shake.reset();
            shake.setFillAfter(true);
            holder.itemView.startAnimation(shake);
        } else if (!showDeleteBtn && data.getNameCode().equals(Common.FUNC_ADD)) {
            holder.itemView.setVisibility(View.GONE);
        } else {
            holder.getView(R.id.iv_delete_btn).setVisibility(View.GONE);
            holder.itemView.setVisibility(View.VISIBLE);
        }
        int deptType = SharedPreferUtil.getInstance().getIntValue(Common.DEPT_TYPE);
        if (deptType == 5 || deptType == 6) {
            if (data.getNameCode().equals(Common.FUNC_QDGL) || data.getNameCode().equals(Common.FUNC_LHSM)) {
                //分拨屏蔽签到管理、揽货扫描
                holder.itemView.setEnabled(false);
            } else {
                holder.itemView.setEnabled(true);
            }
        } else {
            if (data.getNameCode().equals(Common.FUNC_FBZT) || data.getNameCode().equals(Common.FUNC_KYDH)) {
                //网点屏蔽分拨自提、同行卸车
                holder.itemView.setEnabled(false);
            } else {
                holder.itemView.setEnabled(true);
            }
        }
    }

    @Override
    public void onMove(int fromPosition, int toPosition) {
        if (this.dataList.get(fromPosition).getNameCode().equals(Common.FUNC_ADD)
                || this.dataList.get(toPosition).getNameCode().equals(Common.FUNC_ADD)) {
            return;
        }
        if (fromPosition < toPosition) {
            for (int i = fromPosition; i < toPosition; i++) {
                Collections.swap(this.dataList, i, i + 1);
            }
        } else {
            for (int i = fromPosition; i > toPosition; i--) {
                Collections.swap(this.dataList, i, i - 1);
            }
        }
        notifyItemMoved(fromPosition, toPosition);
    }

    @Override
    public void onSwiped(int position) {
        this.dataList.remove(position);
        notifyItemRemoved(position);
    }

    public boolean isShowDeleteBtn() {
        return showDeleteBtn;
    }

    public void setShowDeleteBtn(boolean showDeleteBtn) {
        this.showDeleteBtn = showDeleteBtn;
    }
}
